'''
Unit test suite for the sql_utils module.

Created on Sep 20, 2010

@author: adolfo
'''
from core_test_application_context import CoreTestConfig
from mx.org.came import sql_utils
from springpython.context import ApplicationContext
import logging
import unittest

_LOGGER = logging.getLogger("mx.org.came.sql_utils_test")

class SQLUtilsTest(unittest.TestCase):
    """Class that defines tests for the methods in the module
    ``sql_utils``"""

    # Spring application context with the objects to be used in the
    # unit test suite.
    CONTEXT = ApplicationContext(CoreTestConfig())

    def setUp(self):
        """Does nothing."""
        pass
    
    def tearDown(self):
        """Does nothing"""
        pass

    def test_get_insert_sql(self):
        """Verifies that the function ``_get_insert_sql(decoded_query)``
        generates the expected SQL statements.
        """
        decoded_query = {'Saving': 4000
                         , 'Missing': 0
                         , 'Group ID': 25513
                         , 'Client ID': 9999999
                         , 'schemaName': 'Payment'
                         , 'Week Number': 3
                         , 'dbName': 'CAME'
                         , 'CAME': 393}
        expected_sql_statement = "INSERT INTO [Payment] "\
                "([Client ID],[Saving],[Missing],[Week Number],"\
                "[Group ID],[CAME]) "\
                "VALUES (9999999,4000,0,3,25513,393)"
        sql_statement = sql_utils.get_insert_sql(decoded_query);
        _LOGGER.debug("expected_sql_statement: %s"
                      , expected_sql_statement)
        _LOGGER.debug("sql_statement: %s"
                      , sql_statement)
        self.assertEquals(sql_statement
                          , expected_sql_statement
                          , "Insert SQL statement generated incorrectly!")

    def test_get_select_sql(self):
        """Verifies that the function ``_get_select_sql(decoded_query)``
        generates the expected SQL statements.
        """
        decoded_query = {'schemaName': 'Client', 'Group ID': '=25512'
                         , 'dbName': 'CAME'}
        fetch_list = ['Client ID', 'Group ID', 'Saving', 'CAME Capital'
                     , 'Cycle', 'Group Rate']
        logical = 'AND'
        expected_sql_statement = "SELECT [Client ID], [Group ID], [Saving], "\
                "[CAME Capital], [Cycle], [Group Rate] "\
                "FROM [Client] "\
                "WHERE [Group ID]=25512"
        sql_statement = sql_utils.get_select_sql(decoded_query
                                                 , fetch_list
                                                 , logical)
        self.assertEquals(sql_statement
                          , expected_sql_statement
                          , "Select SQL statement generated incorrectly!")
